// vite.config.ts
import { defineConfig } from 'vite';
import path from 'path';
import dts from 'vite-plugin-dts';
import { terser } from 'rollup-plugin-terser';

export default defineConfig(({ mode }) => {
  const isLibMode = mode === 'lib';
  const isUmdMode = mode === 'umd';
  const isExamplesMode = !isLibMode && !isUmdMode;

  return {
    server: {
      headers: {
        'Cache-Control': 'no-cache, no-store, must-revalidate',
        'Pragma': 'no-cache',
        'Expires': '0'
      }
    },
    plugins: [
      dts({
        include: ['src/**/*'],
        exclude: ['src/**/*.test.ts'],
        outputDir: 'dist/types',
        insertTypesEntry: true,
        copyDtsFiles: true
      })
    ],
    build: isLibMode || isUmdMode ? {
      target: 'es2020',
      outDir: 'dist',
      emptyOutDir: isLibMode, // 只在lib模式下清空目录
      sourcemap: true,
      lib: {
        entry: isUmdMode ? path.resolve(__dirname, 'src/full-import.ts') : path.resolve(__dirname, 'src/index.ts'),
        name: 'ZssLitUi',
        formats: isUmdMode ? ['umd'] : ['es'],
        fileName: (format) => {
          if (isUmdMode) return 'zss-lit-ui.min.js';
          return `zss-lit-ui.${format}.js`;
        }
      },
      rollupOptions: {
        external: ['lit', 'lit-html', 'lit-element'],
        output: {
          globals: {
            lit: 'Lit',
            'lit-html': 'LitHtml',
            'lit-element': 'LitElement'
          },
          plugins: isUmdMode ? [terser()] : [],
          // Tree Shaking优化
          preserveModules: isLibMode,
          preserveModulesRoot: 'src',
          // 按需引入配置
          chunkFileNames: isLibMode ? 'components/[name].js' : '[name].js',
          entryFileNames: isLibMode ? '[name].js' : '[name].[format].js'
        },
        // 按需引入的输入配置
        input: isLibMode ? {
          index: path.resolve(__dirname, 'src/index.ts'),
          'components/basic/index': path.resolve(__dirname, 'src/components/basic/index.ts'),
          'components/form/index': path.resolve(__dirname, 'src/components/form/index.ts'),
          'components/data/index': path.resolve(__dirname, 'src/components/data/index.ts'),
          'components/navigation/index': path.resolve(__dirname, 'src/components/navigation/index.ts'),
          'components/feedback/index': path.resolve(__dirname, 'src/components/feedback/index.ts')
        } : undefined
      },
      cssCodeSplit: true,
      minify: isUmdMode ? 'terser' : false
    } : {
      // examples 构建配置
      outDir: 'dist/examples',
      emptyOutDir: true,
      rollupOptions: {
        input: path.resolve(__dirname, 'examples/index.html'),
      }
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@examples': path.resolve(__dirname, './examples/src'),
      }
    },
    css: {
      preprocessorOptions: {
        css: {
          additionalData: `@import "${path.resolve(__dirname, 'src/styles/variables.css').replace(/\\/g, '/')}";`
        }
      },
      devSourcemap: true
    }
  };
});